package com.wn.controller;

import com.wn.feign.api.UserFeignApi;
import com.wn.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author yellow Docter
 * @date 2023 -07-18
 * @desc
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private UserFeignApi userFeignApi;

    /**
     *
     * redis的操作步骤:
     * 1、添加依赖
     * 2、加配置
     * 3、开启注解
     * 4、在方法上面使用注解
     *      @Cacheable(value = "user:list")  第一次不会查查缓存 会查询数据库、查到数据库存储到redis
     * @return
     */
    @Cacheable(value = "user:list:all")
    @GetMapping("/list")
    public List<User> findUsers() {
        return userFeignApi.findUsers();
    }

    @GetMapping("/page/{currentPage}/{pageSize}")
    public Map pageUsers(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        //远程调用
        Map map = userFeignApi.pageUsers(currentPage, pageSize);
        return map;
    }


    @PostMapping
    public String addUser(@RequestBody User user) {
        String result = userFeignApi.addUser(user);
        return result;
    }

    @PutMapping()
    public String updateUsers(@RequestBody User user) {
        return userFeignApi.updateUsers(user);
    }

    @DeleteMapping("/{id}")
    public String removeUsersById(@PathVariable("id") Integer id) {
        return userFeignApi.findUsers(id);
    }

    @GetMapping("/{id}")
    public User getUserById(@PathVariable("id") Integer id) {
        return userFeignApi.getUserById(id);
    }

}
